package com.gimi.cloud.bbp.merchantZone.consumer.service.store.impl;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.utils.JsonUtil;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.common.utils.reqhead.Util;
import com.gimi.cloud.bbp.common.utils.reqhead.Verification;
import com.gimi.cloud.bbp.dto.store.MerchantRangeDTO;
import com.gimi.cloud.bbp.dto.store.StoreDTO;
import com.gimi.cloud.bbp.merchantZone.consumer.service.store.StoreService;
import com.gimi.cloud.bbp.merchantZone.consumer.feign.StoreFeignService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by shazhiwei on 2017/12/20.
 */
@Service
public class StoreServiceImpl implements StoreService {
    private static Logger logger = LoggerFactory.getLogger(StoreServiceImpl.class);
    private final StoreFeignService storeService;

    @Autowired
    public StoreServiceImpl(StoreFeignService storeService){
        this.storeService = storeService;
    }

    /**
     * 添加商铺
     * @param requestBody
     * @param request
     * @param response
     * @throws Exception
     */
    @Override
    public void addStore(String requestBody, HttpServletRequest request, HttpServletResponse response) throws Exception {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        StoreDTO params = JsonUtil.jsonToBean(requestBody,StoreDTO.class);
        Verification verification = new Verification();
        if(null == params.getMerchantId()){
            verification.setCheckResult(true);
            verification.setMsg("商户ID不能为空");
        }
        if(null == params.getStoreName()){
            verification.setCheckResult(true);
            verification.setMsg("店铺名称不能为空");
        }
        if(null == params.getStoreSimpleDesc()){
            verification.setCheckResult(true);
            verification.setMsg("店铺简介不能为空");
        }
        if(verification.getCheckResult()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,verification.getMsg());
            Util.sendResponse(JsonUtil.toJson(resp), response);
        }else{
            Util.sendResponse(storeService.addStore(params), response);
        }
    }

    /**
     * 获取商铺列表
     * @param requestBody
     * @param request
     * @param response
     * @throws Exception
     */
    @Override
    public void storeList(String requestBody, HttpServletRequest request, HttpServletResponse response) throws Exception {
        logger.info("===>requestData:"+requestBody);
        StoreDTO dto = JsonUtil.jsonToBean(requestBody,StoreDTO.class);
        Util.sendResponse(storeService.storeList(dto), response);
    }

    @Override
    public void allStoreList(String requestBody, HttpServletRequest request, HttpServletResponse response) throws Exception {
        logger.info("===>requestData:"+requestBody);
        StoreDTO dto = JsonUtil.jsonToBean(requestBody,StoreDTO.class);
        Util.sendResponse(storeService.allStoreList(dto), response);
    }

    /**
     * 获取单个店铺
     * @param requestBody
     * @param request
     * @param response
     * @throws Exception
     */
    @Override
    public void storeSingle(String requestBody, HttpServletRequest request, HttpServletResponse response) throws Exception {
        StoreDTO params = JsonUtil.jsonToBean(requestBody,StoreDTO.class);
        Verification verification = new Verification();
        if(null == params.getStoreId()){
            verification.setCheckResult(true);
            verification.setMsg("店铺ID不能为空");
        }
        if(verification.getCheckResult()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,verification.getMsg());
            Util.sendResponse(JsonUtil.toJson(resp), response);
        }else{
            Util.sendResponse(storeService.storeSingle(params), response);
        }
    }

    /**
     * 店铺审核
     * @param requestBody
     * @param request
     * @param response
     * @throws Exception
     */
    @Override
    public void storeAuth(String requestBody, HttpServletRequest request, HttpServletResponse response) throws Exception {
        StoreDTO params = JsonUtil.jsonToBean(requestBody,StoreDTO.class);
        Verification verification = new Verification();
        if(null == params.getStoreId()){
            verification.setCheckResult(true);
            verification.setMsg("店铺ID不能为空");
        }
        if(null == params.getStoreStatus()){
            verification.setCheckResult(true);
            verification.setMsg("店铺状态不能为空");
        }
        if(verification.getCheckResult()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,verification.getMsg());
            Util.sendResponse(JsonUtil.toJson(resp), response);
        }else{
            Util.sendResponse(storeService.storeAuth(params), response);
        }
    }

    /**
     * 图片上传
     */
    @Override
    public void fileUpload(HttpServletResponse response, HttpServletRequest request) throws Exception {
        StandardMultipartHttpServletRequest multipartHttpServletRequest = (StandardMultipartHttpServletRequest) request;
        MultiValueMap multipartFiles = multipartHttpServletRequest.getMultiFileMap();

        Util.sendResponse(storeService.fileUpload(multipartFiles),response);
    }

    /**
     * 更新商铺
     * @param requestBody
     * @param request
     * @param response
     * @throws Exception
     */
    @Override
    public void updateStore(String requestBody, HttpServletRequest request, HttpServletResponse response) throws Exception {
        StoreDTO params = JsonUtil.jsonToBean(requestBody,StoreDTO.class);
        Verification verification = new Verification();
        if(null == params.getMerchantId()){
            verification.setCheckResult(true);
            verification.setMsg("商户ID不能为空");
        }
        if(null == params.getStoreName()){
            verification.setCheckResult(true);
            verification.setMsg("店铺名称不能为空");
        }
        if(null == params.getStoreSimpleDesc()){
            verification.setCheckResult(true);
            verification.setMsg("店铺简介不能为空");
        }
        if(verification.getCheckResult()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,verification.getMsg());
            Util.sendResponse(JsonUtil.toJson(resp), response);
        }else{
            Util.sendResponse(storeService.updateStore(params), response);
        }
    }

    /**
     * 删除商铺
     * @param requestBody
     * @param request
     * @param response
     * @throws Exception
     */
    @Override
    public void deleteStore(String requestBody, HttpServletRequest request, HttpServletResponse response) throws Exception {
        StoreDTO params = JsonUtil.jsonToBean(requestBody,StoreDTO.class);
        Verification verification = new Verification();
        if(null == params.getStoreId()){
            verification.setCheckResult(true);
            verification.setMsg("店铺ID不能为空");
        }
        if(verification.getCheckResult()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,verification.getMsg());
            Util.sendResponse(JsonUtil.toJson(resp), response);
        }else{
            Util.sendResponse(storeService.deleteStore(params), response);
        }
    }

    /**
     * 添加商铺经营范围
     * @param requestBody
     * @param request
     * @param response
     * @throws Exception
     */
    @Override
    public void addMerchantStore(String requestBody, HttpServletRequest request, HttpServletResponse response) throws Exception {
        MerchantRangeDTO dto = JsonUtil.jsonToBean(requestBody,MerchantRangeDTO.class);
        Verification verification = new Verification();
        if(null == dto.getMerchantId()){
            verification.setCheckResult(true);
            verification.setMsg("商户ID不能为空");
        }
        if(null == dto.getStoreId()){
            verification.setCheckResult(true);
            verification.setMsg("店铺ID不能为空");
        }
        if(null == dto.getProdCatId()){
            verification.setCheckResult(true);
            verification.setMsg("店铺经营范围不能为空");
        }
        if(verification.getCheckResult()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,verification.getMsg());
            Util.sendResponse(JsonUtil.toJson(resp), response);
        }else{
            Util.sendResponse(storeService.addMerchantStore(dto), response);
        }
    }

    @Override
    public void merchantStoreAddList(String requestBody, HttpServletRequest request, HttpServletResponse response) throws Exception {
        MerchantRangeDTO dto = JsonUtil.jsonToBean(requestBody,MerchantRangeDTO.class);
        Verification verification = new Verification();
        if(null == dto.getStoreId()){
            verification.setCheckResult(true);
            verification.setMsg("店铺ID不能为空");
        }
        if(verification.getCheckResult()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,verification.getMsg());
            Util.sendResponse(JsonUtil.toJson(resp), response);
        }else{
            Util.sendResponse(storeService.merchantStoreAddList(dto), response);
        }
    }

    @Override
    public void merchantStoreList(String requestBody, HttpServletRequest request, HttpServletResponse response) throws Exception {
        MerchantRangeDTO dto = JsonUtil.jsonToBean(requestBody,MerchantRangeDTO.class);
        Verification verification = new Verification();
        if(null == dto.getStoreId()){
            verification.setCheckResult(true);
            verification.setMsg("店铺ID不能为空");
        }
        if(verification.getCheckResult()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,verification.getMsg());
            Util.sendResponse(JsonUtil.toJson(resp), response);
        }else{
            Util.sendResponse(storeService.merchantStoreList(dto), response);
        }
    }

    @Override
    public void deleteMerchantStore(String requestBody, HttpServletRequest request, HttpServletResponse response) throws Exception {
        logger.info("请求数据:" + JsonUtil.replaceNow(requestBody));
        MerchantRangeDTO dto = JsonUtil.jsonToBean(requestBody,MerchantRangeDTO.class);
        Verification verification = new Verification();
        if(null == dto.getMrId()){
            verification.setCheckResult(true);
            verification.setMsg("经营范围ID不能为空");
        }
        if(verification.getCheckResult()){
            Response resp = new Response();
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR,verification.getMsg());
            Util.sendResponse(JsonUtil.toJson(resp), response);
        }else{
            Util.sendResponse(storeService.deleteMerchantStore(dto), response);
        }
    }

}
